package com.compression.tool.factory;

import com.compression.tool.compression.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 自然醒
 * @version 1.0
 */

//策略模式工厂
public class CompressionStrategyFactory {
    private static final Map<String, CompressionStrategy> strategies = new HashMap<>();

    static {
        //注册策略
        registerStrategy(new ZipCompression());
        registerStrategy(new SevenZipCompression());
        registerStrategy(new TarGzCompression());
        registerStrategy(new RarCompression());
    }


    private static void registerStrategy(CompressionStrategy strategy){
        for (String supportedExtension : strategy.getSupportedExtensions()) {
            strategies.put(supportedExtension, strategy);
        }
    }

    public static CompressionStrategy getStrategy(String extension){
        return strategies.get(extension.toLowerCase());
    }

    public static CompressionStrategy getStrategyByExtension(String extension){
        return strategies.get(extension.toLowerCase());
    }

    public static String[] getSupportedExtensions(){
        return strategies.keySet().toArray(new String[0]);
    }

}
